package me.numark.net;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import android.content.Context;
import android.content.Loader;

import java.util.Map;

/**
 * Created by tian on 14/11/2:下午2:31.
 */
public abstract class BaseGsonLoader<T> extends Loader<T> {

    private static final String TAG = "BaseGsonLoader";

    private String url;

    protected T mResult;

    private boolean mHasDeliveredResult;

    protected volatile boolean mIsLoading;

    private Map<String, String> params;


    /**
     * Stores away the application context associated with context.
     * Since Loaders can be used across multiple activities it's dangerous to
     * store the context directly; always use {@link #getContext()} to retrieve
     * the Loader's Context, don't use the constructor argument directly.
     * The Context returned by {@link #getContext} is safe to use across
     * Activity instances.
     *
     * @param context used to retrieve the application context.
     */
    private Class mClass;
    public BaseGsonLoader(Context context, Class clazz) {
        super(context);
        mIsLoading = false;
        mHasDeliveredResult = false;
        mClass = clazz;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }

    private int method = Request.Method.GET;

    public void setMethod(int method) {
        this.method = method;
    }

    private boolean mNeedsAuthenticate = false;

    public void setNeedsAuthenticate(boolean needsAuthenticate) {
        this.mNeedsAuthenticate = needsAuthenticate;
    }

    protected Response.Listener<T> mListener = new Response.Listener<T>() {

        @Override
        public void onResponse(T response) {
            mResult = response;
            deliverResult(mResult);
            mIsLoading = false;
            mHasDeliveredResult = true;
            //TODO: notify loading progress finished
        }
    };

    protected Response.ErrorListener mErrorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            mIsLoading = false;
            deliverResult(null);
            //TODO: notify loading progress finished, show error
        }
    };

    @Override
    protected void onStartLoading() {
        if (mResult != null) {
            deliverResult(mResult);
        }

        if (!mIsLoading && (mResult == null || takeContentChanged())) {
            forceLoad();
        }
    }

    @Override
    protected void onForceLoad() {
        //load from server
        mIsLoading = true;
        loadData();
        //TODO: show progress
    }

    protected void loadData() {
        GsonRequest request = new GsonRequest<T>(url, method, mClass,
                mNeedsAuthenticate, params, mListener, mErrorListener);
        NetworkHelper.getInstance().addToRequestQueue(request);
        NetworkHelper.getInstance().getRequestQueue().start();
    }

}